import 'package:blocks_blkk/app_configure.dart';
import 'package:blocks_blkk/components/return_button.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart';

/* 
 * 4.0 模块方向设置
 */
class RemoteSetting40Page extends StatefulWidget {
  RemoteSetting40Page({super.key});

  @override
  _RemoteSetting40PageState createState() => _RemoteSetting40PageState();
}

class _RemoteSetting40PageState extends State<RemoteSetting40Page> {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/background.png'),
          fit: BoxFit.fill,
        ),
      ),
      child: Container(
        child: Stack(
          children: [
            ReturnButton(),
            _deviceContainer(),
          ],
        ),
      ),
    );
  }

  Widget _deviceContainer() {
    return Positioned(
      top: ScreenUtil().setHeight(135),
      bottom: ScreenUtil().setHeight(135),
      left: 0,
      right: 0,
      child: Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                  image: AssetImage('assets/set_ble_four.png'),
                  fit: BoxFit.fitHeight),
            ),
          ),
          _switchButtonA(),
          _switchButtonB(),
          _switchButtonC(),
          _switchButtonD(),
        ],
      ),
    );
  }

  Widget _switchImage(int index) {
    return GestureDetector(
      onTap: () {
        setState(() {
          //_switchValue[index] = !_switchValue[index];
          revertSwitchStatus(index);
        });
      },
      child: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(getSwitchStatus(index)
                ? 'assets/set_ble_left.png'
                : 'assets/set_ble_right.png'),
          ),
        ),
      ),
    );
  }

  Widget _switchButtonA() {
    return Positioned(
      left: ScreenUtil().setWidth(70),
      bottom: ScreenUtil().setHeight(70),
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().setWidth(100),
      child: _switchImage(0),
    );
  }

  Widget _switchButtonB() {
    return Positioned(
      left: ScreenUtil().setWidth(70),
      top: ScreenUtil().setHeight(70),
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().setWidth(100),
      child: _switchImage(1),
    );
  }

  Widget _switchButtonC() {
    return Positioned(
      right: ScreenUtil().setWidth(70),
      top: ScreenUtil().setHeight(70),
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().setWidth(100),
      child: _switchImage(2),
    );
  }

  Widget _switchButtonD() {
    return Positioned(
      right: ScreenUtil().setWidth(70),
      bottom: ScreenUtil().setHeight(70),
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().setWidth(100),
      child: _switchImage(3),
    );
  }

  bool getSwitchStatus(int index) {
    switch (index) {
      case 0:
        return AppConfig.instance.channelASpeed == 1;
      case 1:
        return AppConfig.instance.channelBSpeed == 1;
      case 2:
        return AppConfig.instance.channelCSpeed == 1;
      case 3:
        return AppConfig.instance.channelDSpeed == 1;
      default:
        return false;
    }
  }

  void revertSwitchStatus(int index) {
    switch (index) {
      case 0:
        AppConfig.instance.channelASpeed = -AppConfig.instance.channelASpeed;
        break;
      case 1:
        AppConfig.instance.channelBSpeed = -AppConfig.instance.channelBSpeed;
        break;
      case 2:
        AppConfig.instance.channelCSpeed = -AppConfig.instance.channelCSpeed;
        break;
      case 3:
        AppConfig.instance.channelDSpeed = -AppConfig.instance.channelDSpeed;
        break;
    }
    AppConfig.instance.saveToFile();
  }
}
